package com.lovo.sys.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lovo.commom.CscRemote.ICscUserOrderRemote;
import com.lovo.commom.WebRemote.IWebOrderRemote;
import com.lovo.sys.bean.BackInfoBean;
import com.lovo.sys.bean.ProductBean;
import com.lovo.sys.bean.UserOrderBean;
import com.lovo.sys.mapper.IBackInfoMapper;
import com.lovo.sys.mapper.IProductMapper;
import com.lovo.sys.mapper.IUserOrderMapper;
import com.lovo.sys.service.IUserOrderService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

@Service
@Transactional
public class UserOrderServiceImpl implements IUserOrderService {
    @Autowired
    private IProductMapper productMapper;
    @Autowired
    private IUserOrderMapper mapper;
    @Autowired
    private IBackInfoMapper backInfoMapper;
    @Autowired
    private ICscUserOrderRemote cscUserOrderRemote;

    @Autowired
    private IWebOrderRemote webOrderRemote;

    @Override
    public IPage<UserOrderBean> findByItem(Integer pageNum) {
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("uo_delete", 0);
        qw.ne("uo_status", 1);
        return mapper.selectPage(new Page<>(pageNum, 5), qw);
    }

    @Override
    public IPage<UserOrderBean> findAll(Integer pageNum) {
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("uo_status", 1);
        qw.eq("uo_delete", 0);
        return mapper.selectPage(new Page<>(pageNum, 5), qw);
    }

    /**
     * 监听前端订单支付成功的订单对象
     *
     * @param json 订单对象的json字符串
     */
    @RabbitListener(queues = "orderToSscSysQueue")
    public void addUserOrder(String json) {
        UserOrderBean userOrderBean = JSONObject.parseObject(json, UserOrderBean.class);
        mapper.insert(userOrderBean);
    }

    /**
     * 监听客户退货订单
     *
     * @param json
     */

    @RabbitListener(queues = "refundToSysQueue")
    public void addBackInfo(String json) {
        BackInfoBean backInfoBean = JSONObject.parseObject(json, BackInfoBean.class);
        backInfoMapper.insert(backInfoBean);
    }

    @Override
    public UserOrderBean findByUserOrderId(String  idNum) {
        return mapper.findByUserOrderId(idNum);
    }

    @Override
    public void deleteByUserOrderId(long UserOrderId) {
        UserOrderBean userOrderBean = mapper.selectById(UserOrderId);
        userOrderBean.setIsDelete(1);
        mapper.updateById(userOrderBean);
    }

    @Override
    public void addUserOrder(UserOrderBean userOrderBean) {
        mapper.insert(userOrderBean);
    }

    @Override
    public void updateUserOrder(Integer userOrderId, Integer num) {
        UserOrderBean userOrderBean = mapper.selectById(userOrderId);
        userOrderBean.setStatus(num);
        mapper.updateById(userOrderBean);
//远程调用前端借口修改订单状态
        webOrderRemote.updateStatusRemote(userOrderBean.getOrderNum(), 2);

    }

    //退货
    @Override
    public void SalesReturn(Integer userOrderId, Integer num, Integer goodsNum, String goodsName) {
        String idNUm;
        QueryWrapper qw1 = new QueryWrapper<>();
        qw1.eq("pk_orderId", userOrderId);
        UserOrderBean userOrderBean = mapper.selectOne(qw1);
        idNUm = userOrderBean.getOrderNum();
        this.deleteByUserOrderId(userOrderId);
        this.updateUserOrder(userOrderId, num);
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("p_productName", goodsName);
        ProductBean productBean = productMapper.selectOne(qw);
        productBean.setNum(productBean.getNum() + goodsNum);
        productMapper.updateById(productBean);
        //远程调用前端接口修改订单状态
        webOrderRemote.updateStatusRemote(idNUm, 5);
        //远程调用审核接口修好订单状态
        cscUserOrderRemote.CscUpdateStatus(idNUm, 5, "2023-05-12");

    }

    @Override
    public List<String> findAllType() {
        return mapper.findAllType();
    }

    @Override
    public IPage<UserOrderBean> findByDateAndType(String Date, String type, Integer pageNum, Integer status) {
        QueryWrapper qw = new QueryWrapper<>();
        if (Date != null && Date != "") {
            qw.eq("uo_date", LocalDate.parse(Date));
        }
        if (type != null && type != "") {
            qw.eq("uo_type", type);
        }
        if (status != null) {
            qw.eq("uo_status", status);
        }
        qw.eq("uo_delete", 0);
        return mapper.selectPage(new Page<>(pageNum, 5), qw);
    }

    @Override
    public void updateStatus(String orderNum, Integer num, LocalDate payDate) {
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("uo_orderNum", orderNum);
        UserOrderBean userOrderBean = mapper.selectOne(qw);
        userOrderBean.setStatus(num);
        if (payDate != null) {
            userOrderBean.setPayDate(payDate);
        }
        mapper.updateById(userOrderBean);
    }


}
